package javango.contrib.admin.api;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import javango.contrib.hibernate.HibernateUtil;
import javango.db.Managers;

import com.google.inject.Inject;

/**
 * Subclasses should override the constructor calling addClass or addClasses to build the list of classes that can be managed via the admin interface
 * @param clazz
 */
public class ModelAdminOptions implements AdminOptions {

	Map<Class, ModelAdmin> classMap = new LinkedHashMap<Class, ModelAdmin>();
	
	HibernateUtil hibernateUtil;
	Managers managers;
	String appName = "Admin";
	
	public ModelAdminOptions register(Class model, ModelAdmin options) {
		classMap.put(model, options);
		return this;
	}
	
	public ModelAdmin getModelAdmin(Class model) {
		return classMap.get(model);
	}
	
	@Inject
	public ModelAdminOptions(Managers managers, HibernateUtil hibernateUtil) {
		this.hibernateUtil = hibernateUtil;
		this.managers = managers;
	}
	
	public Class getClassMapping(String entityName) {
		for(Class c : classMap.keySet()) {
			if (c.getName().equals(entityName)) {
				return c;
			}
		}
		return null;
	}

	public Iterator<Class> getClassMappings() {
		return classMap.keySet().iterator();
	}

	public String getIndexTemplate() {
		return "javango/contrib/admin/templates/index.ftl";
	}

	public String getAppName() {
		return this.appName;
	}

	public ModelAdminOptions setAppName(String appName) {
		this.appName = appName;
		return this;
	}
}
